package com.qf.realm;

import com.qf.pojo.SysUsers;
import com.qf.service.MenuService;
import com.qf.service.RoleService;
import com.qf.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * @author sunyamei
 * @version V1.0
 * @Project shirodemo
 * @Package com.qf.realm
 * @Description:
 * @Date 2022/7/19 21:28
 *
 * 自定义realm查询数据库.代替shiro.ini
 */
public class MyUserRealm extends AuthorizingRealm {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    //提供授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("22222开始授权");
        //AuthenticationInfo  authenticationInfo=new SimpleAuthenticationInfo(user,password,this.getName());
        SysUsers user =(SysUsers) principalCollection.getPrimaryPrincipal();//user
        System.out.println(user.getId());
        //根据用户id 查找用户角色   验证角色
        Set<String> roles= roleService.findRoles(user.getId());
        //根据用户id 查找用户权限   验证权限
        Set<String> perms= menuService.findPerms(user.getId());
        //授权处理
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        //存储角色
        authorizationInfo.setRoles(roles);
        //存储权限
        authorizationInfo.setStringPermissions(perms);

        return authorizationInfo;
    }
    //提供认证信息（登录 账户）*****
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("111111");//private String username;private char[] password;
        //在token里面对象中用户名是String类型可以直接强转
        String  username =(String) authenticationToken.getPrincipal();
        //在token里面对象中密码是char[] 必须将char[] 转成String类型
        String  password=new String((char[])authenticationToken.getCredentials());
        System.out.println("前端获取的用户名："+username+"密码："+password);
        //根据用户名密码查询数据库 获取用户信息
        //与用户输入的账户和密码进行比较 如果没有问题 登录成功
        SysUsers user = userService.findByUsername(username);
        System.out.println("数据库用户名："+user.getUsername()+"数据库的密码："+password);
        if (user == null) {
            throw new UnknownAccountException("未知用户名");
        }
        if(user.getStatus()==0){
            throw new LockedAccountException("账户已锁定");
        }
        //密码验证
        if(!password.equals(user.getPassword())){// 取servlet里面加密密码 和数据库加密密码进行比对
            throw new CredentialsException("密码错误");
        }

        //存储信息  session (user,password,realmName)
        AuthenticationInfo authenticationInfo=new SimpleAuthenticationInfo(user,password,this.getName());
        return authenticationInfo;
    }
}